%% Copyright 2008 Kevin A. Smith 
%% Licensed under the Apache License, Version 2.0 (the "License"); 
%% you may not use this file except in compliance with the License. 
%% You may obtain a copy of the License at 
%%
%% http://www.apache.org/licenses/LICENSE-2.0 
%%
%% Unless required by applicable law or agreed to in writing, software 
%% distributed under the License is distributed on an "AS IS" BASIS, 
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
%% See the License for the specific language governing permissions and 
%% limitations under the License.

-module(image_response_parser).

-compile([export_all]).

callback({startElement, _, "item", _, _}, {_Current, _Flag, All}) ->
    {[], [], All};
callback({endElement, _, "item", _}, {Current, _Flag, All}) ->
    {[], [], lists:append(All, [Current])};
callback({startElement, _, "imageId", _, _}, {Current, _Flag, All}) ->
    {Current, imageId, All};
callback({endElement, _, "imageId", _}, {Current, _Flag, All}) ->
    {Current, [], All};
callback({startElement, _, "imageLocation", _, _}, {Current, _Flag, All}) ->
    {Current, imageLocation, All};
callback({endElement, _, "imageLocation", _}, {Current, _Flag, All}) ->
    {Current, [], All};
callback({startElement, _, "imageState", _, _}, {Current, _Flag, All}) ->
    {Current, imageState, All};
callback({endElement, _, "imageState", _}, {Current, _Flag, All}) ->
    {Current, [], All};
callback({startElement, _, "imageOwnerId", _, _}, {Current, _Flag, All}) ->
    {Current, imageOwnerId, All};
callback({endElement, _, "imageOwnerId", _}, {Current, _Flag, All}) ->
    {Current, [], All};
callback({startElement, _, "isPublic", _, _}, {Current, _Flag, All}) ->
    {Current, isPublic, All};
callback({endElement, _, "isPublic", _}, {Current, _Flag, All}) ->
    {Current, [], All};
callback({endElement, _, "imagesSet", _}, {_Current, _Flag, All}) ->
    {ok, lists:filter(fun drop_empty/1, lists:reverse(All))};
callback({characters, Text}, {Current, Flag, All}) when is_atom(Flag) ->
    Current1 = lists:append(Current, [{Flag, Text}]),
    {Current1, Flag, All};

callback(_Event, State) ->
    State.

drop_empty(I) ->
	length(I) > 0.